/*
 * principal.java
 *
 * Created on January 31, 2008, 8:43 PM
 */

package exportator;

import bd.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import classes.Pessoa;
import classes.Util;
import classes.Produto;

/**
 *
 * @author  José Rogério
 */
public class Principal extends javax.swing.JFrame {
    
    private String nameFilePostgres= "postgres.xml";
    private String nameFileInterbase= "interbase.xml";
    Database bdPostgres = new Database();
    Database bdInterbase = new Database();
    Settings settings = new Settings();
    
    public Principal() {
        initComponents();
    }
    
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jPanel1 = new javax.swing.JPanel();
      btnConfigurarPostgres = new javax.swing.JButton();
      btnLimparPostgres = new javax.swing.JButton();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      lblHostPostgres = new javax.swing.JLabel();
      lblBancoPostgres = new javax.swing.JLabel();
      btnCarregarPostgres = new javax.swing.JButton();
      lblProcessing = new javax.swing.JLabel();
      jPanel3 = new javax.swing.JPanel();
      btnImportar = new javax.swing.JButton();
      ckbClientes = new javax.swing.JCheckBox();
      ckbProdutos = new javax.swing.JCheckBox();
      ckbFornecedores = new javax.swing.JCheckBox();
      jPanel4 = new javax.swing.JPanel();
      jScrollPane1 = new javax.swing.JScrollPane();
      txtProgresso = new javax.swing.JTextArea();
      lblMensagem = new javax.swing.JLabel();
      jPanel2 = new javax.swing.JPanel();
      btnConfigurarInterbase = new javax.swing.JButton();
      btnLimparInterbase = new javax.swing.JButton();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      lblHostInterbase = new javax.swing.JLabel();
      lblBancoInterbase = new javax.swing.JLabel();
      btnCarregarInterbase = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Postgres"));

      btnConfigurarPostgres.setText("Configurar");
      btnConfigurarPostgres.setName("btnConfigurarPostgres"); // NOI18N
      btnConfigurarPostgres.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnConfigurarPostgresActionPerformed(evt);
         }
      });

      btnLimparPostgres.setText("Limpar");
      btnLimparPostgres.setName("btnLimparPostgres"); // NOI18N
      btnLimparPostgres.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLimparPostgresActionPerformed(evt);
         }
      });

      jLabel1.setText("Banco:");

      jLabel2.setText("Host:");

      lblHostPostgres.setText("/dev/null");
      lblHostPostgres.setName("lblHostPostgres"); // NOI18N

      lblBancoPostgres.setText("/guest/output/text/xxx/");
      lblBancoPostgres.setName("lblBancoPostgres"); // NOI18N

      btnCarregarPostgres.setText("Carregar");
      btnCarregarPostgres.setName("btnCarregarPostgres"); // NOI18N
      btnCarregarPostgres.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCarregarPostgresActionPerformed(evt);
         }
      });

      lblProcessing.setIcon(new javax.swing.ImageIcon("home/user/NetBeansProjects/exportator/images/loading.gif")); // NOI18N

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel1Layout.createSequentialGroup()
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jLabel2)
                     .addComponent(jLabel1))
                  .addGap(18, 18, 18)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(lblHostPostgres)
                     .addComponent(lblBancoPostgres))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
                  .addComponent(lblProcessing)
                  .addGap(126, 126, 126))
               .addGroup(jPanel1Layout.createSequentialGroup()
                  .addComponent(btnConfigurarPostgres)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(btnCarregarPostgres)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(btnLimparPostgres)
                  .addContainerGap(386, Short.MAX_VALUE))))
      );
      jPanel1Layout.setVerticalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(lblHostPostgres, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(lblBancoPostgres, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnConfigurarPostgres)
               .addComponent(btnCarregarPostgres)
               .addComponent(btnLimparPostgres))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(30, Short.MAX_VALUE)
            .addComponent(lblProcessing)
            .addGap(51, 51, 51))
      );

      jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

      btnImportar.setText("Importar");
      btnImportar.setName("btnImportar"); // NOI18N
      btnImportar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnImportarActionPerformed(evt);
         }
      });

      ckbClientes.setText("Clientes");

      ckbProdutos.setText("Produtos");

      ckbFornecedores.setText("Fornecedores");

      javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
      jPanel3.setLayout(jPanel3Layout);
      jPanel3Layout.setHorizontalGroup(
         jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(ckbFornecedores)
            .addGap(28, 28, 28)
            .addComponent(ckbClientes)
            .addGap(18, 18, 18)
            .addComponent(ckbProdutos)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 216, Short.MAX_VALUE)
            .addComponent(btnImportar)
            .addContainerGap())
      );
      jPanel3Layout.setVerticalGroup(
         jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel3Layout.createSequentialGroup()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel3Layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(btnImportar))
               .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                  .addComponent(ckbFornecedores)
                  .addComponent(ckbClientes)
                  .addComponent(ckbProdutos)))
            .addContainerGap(28, Short.MAX_VALUE))
      );

      jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Progresso"));

      txtProgresso.setColumns(20);
      txtProgresso.setRows(5);
      jScrollPane1.setViewportView(txtProgresso);

      javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
      jPanel4.setLayout(jPanel4Layout);
      jPanel4Layout.setHorizontalGroup(
         jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel4Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(lblMensagem)
            .addContainerGap())
         .addGroup(jPanel4Layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
            .addGap(36, 36, 36))
      );
      jPanel4Layout.setVerticalGroup(
         jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel4Layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(10, 10, 10)
            .addComponent(lblMensagem)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );

      jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Interbase"));

      btnConfigurarInterbase.setText("Configurar");
      btnConfigurarInterbase.setName("btnConfigurarInterbase"); // NOI18N
      btnConfigurarInterbase.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnConfigurarInterbaseActionPerformed(evt);
         }
      });

      btnLimparInterbase.setText("Limpar");
      btnLimparInterbase.setName("btnLimparInterbase"); // NOI18N
      btnLimparInterbase.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLimparInterbaseActionPerformed(evt);
         }
      });

      jLabel3.setText("Banco:");

      jLabel4.setText("Host:");

      lblHostInterbase.setText("/home/user/rogerio/cust/binder");
      lblHostInterbase.setName("lblHostInterbase"); // NOI18N

      lblBancoInterbase.setText("/nemesis/layer/");
      lblBancoInterbase.setName("lblBancoInterbase"); // NOI18N

      btnCarregarInterbase.setText("Carregar");
      btnCarregarInterbase.setName("btnCarregarInterbase"); // NOI18N
      btnCarregarInterbase.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCarregarInterbaseActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
      jPanel2.setLayout(jPanel2Layout);
      jPanel2Layout.setHorizontalGroup(
         jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel2Layout.createSequentialGroup()
                  .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jLabel4)
                     .addComponent(jLabel3))
                  .addGap(8, 8, 8)
                  .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(lblHostInterbase)
                     .addComponent(lblBancoInterbase)))
               .addGroup(jPanel2Layout.createSequentialGroup()
                  .addComponent(btnConfigurarInterbase)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(btnCarregarInterbase)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(btnLimparInterbase)))
            .addContainerGap(345, Short.MAX_VALUE))
      );
      jPanel2Layout.setVerticalGroup(
         jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(lblHostInterbase, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel3)
               .addComponent(lblBancoInterbase, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnConfigurarInterbase)
               .addComponent(btnLimparInterbase)
               .addComponent(btnCarregarInterbase)))
      );

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void btnConfigurarPostgresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigurarPostgresActionPerformed
        new EditSettings(nameFilePostgres).setVisible(true);
}//GEN-LAST:event_btnConfigurarPostgresActionPerformed

    private void btnConfigurarInterbaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigurarInterbaseActionPerformed
        new EditSettings(nameFileInterbase).setVisible(true);
    }//GEN-LAST:event_btnConfigurarInterbaseActionPerformed

    private void btnCarregarInterbaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregarInterbaseActionPerformed
        if( Settings.settingsExists(nameFileInterbase))
        {
            if( settings.readSettings(nameFileInterbase))
            {
                lblHostInterbase.setText(settings.getDatabaseHost());
                lblBancoInterbase.setText(settings.getDatabaseName());
                bdInterbase.settings = settings;
                if( !bdInterbase.connect())
                    JOptionPane.showMessageDialog(null, bdInterbase.ErrorMessage);
            }
        }
    }//GEN-LAST:event_btnCarregarInterbaseActionPerformed

    private void btnCarregarPostgresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregarPostgresActionPerformed
        if( Settings.settingsExists(nameFilePostgres))
        {
            if( settings.readSettings(nameFilePostgres))
            {
                lblHostPostgres.setText(settings.getDatabaseHost());
                lblBancoPostgres.setText(settings.getDatabaseName());
                bdPostgres.settings = settings;
                if( !bdPostgres.connect())
                    JOptionPane.showMessageDialog(null, bdPostgres.ErrorMessage);
            }
        }
    }//GEN-LAST:event_btnCarregarPostgresActionPerformed

    private void btnLimparPostgresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparPostgresActionPerformed
        if(JOptionPane.showConfirmDialog(null, "You really clear this settings?") != JOptionPane.OK_OPTION)
            return;
        if( settings.clearSettings(nameFilePostgres))
        {
            lblHostPostgres.setText("");
            lblBancoPostgres.setText("");
        }
    }//GEN-LAST:event_btnLimparPostgresActionPerformed

    private void btnLimparInterbaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparInterbaseActionPerformed
        if(JOptionPane.showConfirmDialog(null, "Do You really clear this settings?") != JOptionPane.OK_OPTION)
            return;        
        if( settings.clearSettings(nameFileInterbase))
        {
            lblHostInterbase.setText("");
            lblBancoInterbase.setText("");
        }
    }//GEN-LAST:event_btnLimparInterbaseActionPerformed

    private void btnImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImportarActionPerformed
        lblMensagem.setText("While a moment guy, I will export this garbage!\n");
        lblProcessing.setVisible(true);
        this.repaint();
        try
        {
           if( ckbFornecedores.isSelected())
           {
              exportGarbageFornecedores();
           }
           
           if( ckbClientes.isSelected())
           {
              exportGarbageClientes();
           }
           
           if( ckbProdutos.isSelected())
           {
              exportGarbageProdutos();
           }
        }
        catch(Exception e)
        {
           txtProgresso.append("I think, so I exists, but one error occur: "+e.toString()+"\n");
        }

        lblMensagem.setText("Thank you for wait, have a nice day!\n");
    }//GEN-LAST:event_btnImportarActionPerformed
    
    
    private void exportGarbageFornecedores()
    {
        String sqlInterbase;
        Pessoa fornecedor;
        int count = 0;
              
        try
        {
            sqlInterbase = "SELECT * FROM FORNECEDOR;";  
            fornecedor = new Pessoa(bdPostgres);
            ResultSet dsTempInterbase = bdInterbase.executeQueryRs(sqlInterbase);

            bdPostgres.beginTransaction();

            txtProgresso.append("Exporting table FORNECEDOR FROM database of TEMA!\n");
            
            while(dsTempInterbase.next())
            {
                count++;
                txtProgresso.append("Register number: "+count+" of many!\n");       
                 this.repaint();
                 
                fornecedor.setCodigo    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CODIGO")));
                fornecedor.setNome      (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("NOME")));	   
                fornecedor.setEndereco  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ENDERECO")));
                fornecedor.setBairro    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("BAIRRO")));	
                fornecedor.setCidade    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CIDADE")));	
                //(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CONTATO")));	
                fornecedor.setEmail     (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("EMAIL")));	
                fornecedor.setEstado    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ESTADO")));	
                fornecedor.setCep       (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CEP")));		
                fornecedor.setCpf       (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CNPJ")));		
                fornecedor.setInsc      (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("INSC")));		
                fornecedor.setFone      (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FONE")));		
                fornecedor.setFax       (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FAX")));		
                fornecedor.setDatnasc   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("NASCIMENTO")));	
                fornecedor.setUCompra   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("UCOMPRA")));		
                //(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("VCOMPRA")));	

                if( !fornecedor.saveFornecedor(false))
                {
                    txtProgresso.append("\nErro fii da peste: "+fornecedor.getMsgErro()+"\n");
                    bdPostgres.rollbackTransaction();
                    break;
                }
            }

            bdPostgres.commitTransaction();
        }
        catch(SQLException e)
        {
            bdPostgres.rollbackTransaction();
        }
    }
    
    private void exportGarbageClientes() 
    {
        String sqlInterbase;
        Pessoa cliente;
        int count = 0;
              
        try
        {
            sqlInterbase = "SELECT * FROM CLIENTES;";  
            cliente = new Pessoa(bdPostgres);
            ResultSet dsTempInterbase = bdInterbase.executeQueryRs(sqlInterbase);

            bdPostgres.beginTransaction();

            txtProgresso.append("Exporting table CLIENTES FROM database of TEMA!");
            
            while(dsTempInterbase.next())
            {
                count++;
                txtProgresso.append("\nRegister number: "+count+" of many!");
                this.repaint();
                
                cliente.setCodigo	(Util.workArroundUtf8ToUnicode(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CODIGO"))));		
                cliente.setNome		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("NOME")));			
                cliente.setFantasia	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FANTASIA")));	
                cliente.setEndereco	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ENDERECO")));	
                cliente.setBairro	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("BAIRRO")));		
                cliente.setCidade	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CIDADE")));		
                cliente.setProfisao	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PROFISAO")));	
                cliente.setConjuge	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CONJUGE")));		
                cliente.setPai		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PAI")));			
                cliente.setMae		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("MAE")));			
                cliente.setTrabalho	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("TRABALHO")));	
                cliente.setRefere1	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("REFERE1")));		
                cliente.setRefere2	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("REFERE2")));		
                cliente.setEmail	   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("EMAIL")));		
                cliente.setVendedor	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("VENDEDOR")));	
                cliente.setConvenio	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CONVENIO")));	
                cliente.setCredito	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CREDITO")));		
                cliente.setEstado	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ESTADO")));		
                cliente.setUsuario	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("USUARIO")));		
                cliente.setLiberado	(Util.workArroundBoolean(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("LIBERADO"))));	
                cliente.setCep		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CEP")));			
                cliente.setCpf		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CPF")));			
                cliente.setRg		   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("RG")));			
                cliente.setFone		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FONE")));			
                cliente.setFax		(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FAX")));			
                cliente.setFonetrab	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FONETRAB")));	
                cliente.setFonerefe	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FONEREFE")));	
                cliente.setFoneref1	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FONEREF1")));	
                cliente.setAtivo	   (Util.workArroundBoolean(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ATIVO"))));		
                cliente.setPrefere	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PREFERE")));		
                cliente.setCadspc	(Util.workArroundBoolean(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CADSPC"))));		
                cliente.setBlqalto	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("BLQALTO")));		
                cliente.setCadastro	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CADASTRO")));	
                cliente.setDatnasc	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DATNASC")));		
                cliente.setDatcomp	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DATCOMP")));		
                cliente.setDatbloq	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DATBLOQ")));		
                cliente.setDiaven	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DIAVEN")));	
                cliente.setAtraso	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ATRASO")));		
                cliente.setSalario	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("SALARIO")));		
                cliente.setLimite	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("LIMITE")));		
                cliente.setCompras	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("COMPRAS")));		
                cliente.setVcompra	(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("VCOMPRA")));		
                cliente.setFicha	   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FICHA")));

                
                if( !cliente.saveCliente(false))
                {
                    txtProgresso.append("\nErro fii da peste: "+cliente.getMsgErro());
                    bdPostgres.rollbackTransaction();
                    break;
                }
            }

            bdPostgres.commitTransaction();
        }
        catch(SQLException e)
        {
            bdPostgres.rollbackTransaction();
        }
    }
    
    
    private void exportGarbageProdutos()
    {
        String sqlInterbase;
        Produto produto;
        int count = 0;
              
        try
        {
            sqlInterbase =  " SELECT PRODUTOS.*, MARCAS.NOME AS MARCANOME, GRUPOS.NOME AS GRUPONOME, SUBGRUPO.NOME AS SUBGRUPONOME "
                           +" FROM PRODUTOS "
                           +" JOIN MARCAS ON MARCAS.CODIGO = PRODUTOS.MARCA "
                           +" JOIN GRUPOS ON GRUPOS.CODIGO = PRODUTOS.GRUPO "
                           +" JOIN SUBGRUPO ON SUBGRUPO.CODIGO = PRODUTOS.SGRUPO;";  
            produto = new Produto(bdPostgres);
            ResultSet dsTempInterbase = bdInterbase.executeQueryRs(sqlInterbase);

            bdPostgres.beginTransaction();

            txtProgresso.append("Exporting table PRODUTOS FROM database of TEMA!");
            
            while(dsTempInterbase.next())
            {
                count++;
                txtProgresso.append("\nRegister number: "+count+" of many!");       
                this.repaint();
                
                  produto.setCodigo   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CODIGO")));
                  produto.setCodbar   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CODBAR")));	
                  produto.setRefere   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("REFERE")));	
                  produto.setCodncm   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CODNCM")));	
                  produto.setNome     (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("NOME")));		
                  produto.setNecf     (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("NECF")));		
                  produto.setSecao    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("SECAO")));	
                  produto.setUnidade  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("UNIDADE")));	
                  produto.setGrupo    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("GRUPONOME")));	
                  produto.setSgrupo   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("SUBGRUPONOME")));	
                  produto.setMarca    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("MARCANOME")));	
                  produto.setAlicota  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ALICOTA")));	
                  produto.setSituacao (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("SITUACAO")));
                  produto.setFornece  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FORNECE")));	
                  produto.setPneto    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PNETO")));	
                  produto.setPcusto   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PCUSTO")));	
                  produto.setPmedio   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PMEDIO")));	
                  produto.setPvenda   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PVENDA")));	
                  produto.setVenda2   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("VENDA2")));	
                  produto.setMargem   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("MARGEM")));	
                  produto.setIcmcom   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ICMCOM")));	
                  produto.setIcmven   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ICMVEN")));	
                  produto.setFrete    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FRETE")));	
                  produto.setDicms    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DICMS")));	
                  produto.setIpi      (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("IPI")));		
                  produto.setOutros   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("OUTROS")));	
                  produto.setComisao  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("COMISAO")));
                  produto.setDesconto (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DESCONTO")));
                  produto.setMinimo   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("MINIMO")));	
                  produto.setMaximo   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("MAXIMO")));	
                  produto.setPedido   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PEDIDO")));	
                  produto.setEstoque  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("ESTOQUE")));	
                  produto.setDeposito (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DEPOSITO")));
                  produto.setUcompra  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("UCOMPRA")));	
                  produto.setSaldo    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("SALDO")));	
                  produto.setEcompra  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("E_COMPRA")));
                  produto.setEvenda   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("E_VENDA")));	
                  produto.setCadastro (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("CADASTRO")));
                  produto.setDcompra  (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("DCOMPRA")));	
                  produto.setPvalte   (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("PVALTE")));	
                  produto.setFicha    (Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("FICHA")));	
                //(Util.workArroundUtf8ToUnicode(dsTempInterbase.getString("VCOMPRA")));	

                if( !produto.saveProduto(false))
                {
                    txtProgresso.append("\nErro fii da peste: "+produto.getMsgErro());
                    bdPostgres.rollbackTransaction();
                    break;
                }
            }

            bdPostgres.commitTransaction();
        }
        catch(SQLException e)
        {
            bdPostgres.rollbackTransaction();
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
    
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnCarregarInterbase;
   private javax.swing.JButton btnCarregarPostgres;
   private javax.swing.JButton btnConfigurarInterbase;
   private javax.swing.JButton btnConfigurarPostgres;
   private javax.swing.JButton btnImportar;
   private javax.swing.JButton btnLimparInterbase;
   private javax.swing.JButton btnLimparPostgres;
   private javax.swing.JCheckBox ckbClientes;
   private javax.swing.JCheckBox ckbFornecedores;
   private javax.swing.JCheckBox ckbProdutos;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   private javax.swing.JPanel jPanel3;
   private javax.swing.JPanel jPanel4;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JLabel lblBancoInterbase;
   private javax.swing.JLabel lblBancoPostgres;
   private javax.swing.JLabel lblHostInterbase;
   private javax.swing.JLabel lblHostPostgres;
   private javax.swing.JLabel lblMensagem;
   private javax.swing.JLabel lblProcessing;
   private javax.swing.JTextArea txtProgresso;
   // End of variables declaration//GEN-END:variables
    
}
